package com.tanger.common.exception;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 所有Controller基类，均需继承此抽象类
 * @author Hchenbin
 */
public abstract class AbstractController {

	private static final Logger log = LoggerFactory.getLogger(AbstractController.class);
	
	/** 基于@ExceptionHandler异常处理 */
	//(value = { BusinessException.class, ParameterException.class, Exception.class})
	@ExceptionHandler
	public String exception(HttpServletRequest request, HttpServletResponse response, Exception e) {
		e.printStackTrace();
		log.info("interface", e);
		response.setContentType("application/json;charset=utf-8");
		try {
			PrintWriter out = response.getWriter();
			JSONObject data = new JSONObject();
			data.put("msg", "Interface error, please refer to API interface description!");
			data.put("code", 400);
			out.print(data);
			out.close();
		} catch (IOException e1) {
			e1.printStackTrace();
			log.error("error1", e1);
		}
		return null;
	}
}